package com.ziytek.web.citizen.model.rsp;

import com.ziytek.web.citizen.enums.ResponseEnum;
import lombok.Data;

import java.util.Objects;

/**
 * @author fenghx
 * @version v1.0
 * @className LoginInResponse
 * @description 功能描述
 * @date 2023/8/14 10:07
 **/
@Data
public class LogInResponse {
    /**
     * 返回编码
     */
    private String retCode;

    /**
     * 编码描述
     */
    private String retMsg;

    /**
     * 用户登录token
     */
    private String token;

    /**
     * 用户角色 user-普通用户 admin-管理员
     */
    private String role;


    public void resultSuccess(String token, String role) {
        if (Objects.nonNull(token) && !token.startsWith(ResponseEnum.ERROR_START.getCode())) {
            this.setToken(token);
            this.setRole(role);
            this.setRetCode(ResponseEnum.SUCCESS.getCode());
            this.setRetMsg(ResponseEnum.SUCCESS.getMsg(token));
        }
    }

    public void resultError(String token) {
        if (Objects.nonNull(token) && token.startsWith(ResponseEnum.ERROR_START.getCode())) {
            this.setRetCode(token);
            this.setRole(ResponseEnum.ROLE_USER.getCode());
            this.setRetMsg("登录失败");
        }
    }
}
